#!/usr/bin/env python

import os, sys
import numpy as np
from argparse import ArgumentParser
#from cosat.ccd import *
from cosat.ObsCoverageStats import *
from cosat.utils import *

def runHealPixStats():
    parser = ArgumentParser()
    parser.add_argument('--seq', '-s', default=None, type=str,
                        help='Specify filename of the observation sequence.')
    parser.add_argument('--outdir', '-o', default='tmp', type=str,
                        help='Specify output dir name, if not provided, the output dir will be tmp by default.')
    parser.add_argument('--nside', '-n', default=512, type=int,
                        help='Specify NSIDE for Healpy map [default: 512]')
    parser.add_argument('--debug', '-d', action='store_true',
                        help='If true, running in debug mode, run stats for the first 10,000 observations only [default: False].')
    parser.add_argument('--query_method', '-q', default=0, type=int,
                        help='Choose which method is to be used to search pixels inside a CCD during an exposure. [Default: 0--query_polygon]')
    parser.add_argument('--query_inclusive', '-i', action='store_true',
                        help='If False, return the exact set of pixels whose pixel centers lie within the polygon; if True, return all pixels \
                        that overlap with the polygon, and maybe a few more. [Default: False]')
    parser.add_argument('--healpix_map_yrs', '-y', nargs='+', default=None,
                        help='Specify years to output the healpix map(s). [Default: None]')
    parser.add_argument('--write_band_cover_map', '-b', action='store_true',
                        help='If true, write visit number counts map in fits format (healpix map) for each band. [Default: false]')

    args = parser.parse_args()

    ocs = ObsCoverageStats( nside=int(args.nside), \
                            debug_mode=args.debug, \
                            query_method=args.query_method, \
                            query_inclusive=args.query_inclusive, \
                            healpix_map_yrs=args.healpix_map_yrs, \
                            write_band_cover_map=args.write_band_cover_map, \
                            outdir=args.outdir )
    ocs.load_obs_seq( args.seq );
    ocs.run_stats( )
    # ocs.print_res()

if __name__ == '__main__':
    runHealPixStats()
